
const stringSimilarity = require('string-similarity-js').default

function isValid(before, after, similarity) {
  // console.log(before, after)
  if(!before || !after) return true
  const _similarity = stringSimilarity(before, after)
  return _similarity < similarity
}

function handleSimilarity(commit, similarity) {
  console.log('commit', commit.sha)
  console.log('files', commit.files.map(i => i.name))
  commit.files = commit.files.filter(fileInfo => isValid(fileInfo.diff.contentBefore,fileInfo.diff.contentAfter, similarity))
  commit.insertions = 0
  commit.deletions = 0
  commit.files.forEach(fileInfo => {
    commit.insertions += fileInfo.insertions
    commit.deletions += fileInfo.deletions
  })
  return commit
}

module.exports = handleSimilarity